home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / GRAHAM / XAAES_S.ZIP / XAAES / RESOURCE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-29  |  12.7 KB  |  475 lines

  1. /*
  2.  * XaAES - XaAES Ain't the AES
  3.  *
  4.  * A multitasking AES replacement for MiNT
  5.  *
  6.  */
  7.  
  8. #include <FCNTL.H>
  9. #include <STDIO.H>
  10. #include <OSBIND.H>
  11. #include <VDI.H>
  12. #include <memory.h>
  13. #include "K_DEFS.H"
  14. #include "RESOURCE.H"
  15. #include "XA_GLOBL.H"
  16. #include "XA_DEFS.H"
  17. #include "XA_TYPES.H"
  18.  
  19. /*
  20.     RESOURCE FILE HANDLER
  21.  
  22.     Simulate the standard GEM AES resource access functions.
  23.     
  24.     I've added these to Steve's routines to act as a bit of an interface
  25.     to his routines. Each application has it's own std_resource as part
  26.     of the XA_CLIENT structure. As Steve's resource loader is happy to
  27.     allow multiple resource files, I should add some extra calls to support it.
  28. */
  29.  
  30.  
  31. unsigned long XA_rsrc_load(short clnt_pid,AESPB *pb)
  32. {
  33.     char *n=(char*)(pb->addrin[0]);
  34.     char full_path[180];
  35.     void **glob_entry;
  36.     short f;
  37.  
  38. /* If the client is overwriting it's existing resource then better free it
  39.      (it shouldn't, but just in case) */
  40. /* I don't think this is a good idea - much better to have a memory leak
  41.     than a process continuing to access a free'd memory region; GEM AES
  42.     doesn't auto-free an existing RSC either, so this would be
  43.     incompatible anyway... <mk>
  44. */
  45. #if 0
  46.      if (clients[clnt_pid].std_resource)
  47.          FreeResources(clients[clnt_pid].std_resource);
  48. #endif
  49.  
  50. /* What sort of path is it? */
  51.     for(f=0; (n[f])&&(n[f]!='\\'); f++);
  52.     if (n[f])
  53.         sprintf(full_path,"%s",n);
  54.     else
  55.         sprintf(full_path,"%s\\%s",clients[clnt_pid].home_path,n);
  56.  
  57.     DIAGS(("rsrc_load('%s')\n",full_path));
  58.  
  59.     clients[clnt_pid].std_resource=LoadResources(full_path, DU_RSX_CONV, DU_RSY_CONV);
  60.     if (clients[clnt_pid].std_resource)
  61.         pb->intout[0]=1;
  62.     else
  63.         pb->intout[0]=0;
  64.  
  65.     DIAGS((" LoadResources returned %d\n",clients[clnt_pid].std_resource));
  66.  
  67. /* Fill in the application's global array with a pointer to the resource */
  68.     glob_entry=(void**)(clients[clnt_pid].globl_ptr+5);
  69.     *glob_entry=clients[clnt_pid].std_resource;
  70.  
  71.     return XAC_DONE;
  72. }
  73.  
  74. unsigned long XA_rsrc_free(short clnt_pid,AESPB *pb)
  75. {
  76.     if (clients[clnt_pid].std_resource)
  77.     {
  78.         FreeResources(clients[clnt_pid].std_resource);
  79.         clients[clnt_pid].std_resource=NULL;
  80.         pb->intout[0]=1;
  81.     }else{
  82.         pb->intout[0]=0;
  83.     }
  84.  
  85.     return XAC_DONE;
  86. }
  87.  
  88. unsigned long XA_rsrc_gaddr(short clnt_pid,AESPB *pb)
  89. {
  90.     OBJECT **tree_addr;
  91.     char **text_addr;
  92.     void **image_addr,**glob;
  93.     short type=pb->intin[0], index=pb->intin[1];
  94.  
  95.     if (!clients[clnt_pid].std_resource)
  96.     {
  97.         glob=(void**)(clients[clnt_pid].globl_ptr+5);
  98.         clients[clnt_pid].std_resource=*glob;
  99.     }
  100.     
  101.     switch(type)
  102.     {
  103.         case R_TREE:
  104.             tree_addr=(OBJECT**)pb->addrout;
  105.             *tree_addr=ResourceTree(clients[clnt_pid].std_resource,index);
  106.             break;
  107.         case R_STRING:
  108.             text_addr=(char**)pb->addrout;
  109.             *text_addr=ResourceString(clients[clnt_pid].std_resource,index);
  110.             break;
  111.         case R_IMAGEDATA:
  112.             image_addr=(void**)pb->addrout;
  113.             *image_addr=ResourceImage(clients[clnt_pid].std_resource,index);
  114.             break;
  115.     }
  116.     
  117.     if (pb->addrout[0])
  118.         pb->intout[0]=1;
  119.     else
  120.         pb->intout[0]=0;
  121.         
  122.     return XAC_DONE;
  123. }
  124.  
  125. /*
  126.  *    Fixup OBJECT coordinates; the lower byte contains a (character-based)
  127.  *    coordinate, the upper byte an additional (pixel) offset.
  128.  */
  129. #define fixup(val,scale) (((val)&0xff)*(scale)+(((val)>>8)&0xff))
  130.  
  131. void obfix(OBJECT *tree, short object)
  132. {
  133.     OBJECT *o = &tree[object];
  134.  
  135.     o->ob_x = fixup(o->ob_x, display.c_max_w) ;
  136.     o->ob_y = fixup(o->ob_y, display.c_max_h) ;
  137.     /*
  138.     * Special case handling: any OBJECT 80 characters wide is supposed
  139.     * to be a menu bar, which always covers the entire screen width...
  140.     */
  141.     o->ob_width = (o->ob_width==80) ?
  142.             display.w : fixup(o->ob_width, display.c_max_w) ;
  143.     o->ob_height = fixup(o->ob_height, display.c_max_h) ;
  144. }
  145.  
  146. unsigned long XA_rsrc_obfix(short clnt_pid,AESPB *pb)
  147. {
  148.     obfix((OBJECT*)pb->addrin[0],pb->intin[0]);
  149.     pb->intout[0]=1;
  150.     return XAC_DONE;
  151. }
  152.  
  153. /*
  154.     Code in this module is based on the resource loader from
  155.     Steve Sowerby's AGiLE library. Thanks to Steve for allowing
  156.     me to use his code.
  157. */
  158.  
  159. short resWidth,resHeight;
  160.  
  161. /*
  162.     FixColourIconData : Convert a colour icon from device independent to device specific
  163. */
  164. void FixColourIconData(CICONBLK *icon)
  165. {
  166.     CICON *c;
  167.     MFDB src,dst;
  168.     unsigned long len=(((icon->monoblk.ib_wicon+15)>>3)&~1) * icon->monoblk.ib_hicon;
  169.     unsigned long old_len,new_len,l;
  170.     short *new_data;
  171.  
  172.     for(c=icon->mainlist; c; c=c->next_res)
  173.     {
  174.         
  175.         if (c->num_planes!=display.planes)
  176.         {
  177.             old_len  = c->num_planes * len;
  178.             new_len = display.planes * len;
  179.         
  180.             new_data = (short*)Mxalloc(new_len+10,MX_STRAM);
  181.             
  182.             for(l=0; l<(old_len+1)>>1; l++)
  183.                 new_data[l]=c->col_data[l];
  184.             for(; l<(new_len+1)>>1; l++)
  185.                 new_data[l]=0;
  186.             
  187.             c->col_data=new_data;
  188.         }
  189.         
  190.         src.fd_w = icon->monoblk.ib_wicon;                /*Transform standard icon*/
  191.         src.fd_h = icon->monoblk.ib_hicon;
  192.         src.fd_wdwidth = icon->monoblk.ib_wicon>>4;
  193.         src.fd_stand = 1;
  194.         src.fd_r1 = src.fd_r2 = src.fd_r3 = 0;
  195.         src.fd_nplanes = display.planes;
  196.         dst.fd_stand = 0;
  197.         src.fd_addr = c->col_data;
  198.         dst = src;
  199.         vr_trnfm(V_handle,&src,&dst);
  200.  
  201.         if (c->sel_data)                            /*Transform selected icon if exists*/
  202.         {
  203.             
  204.             if (c->num_planes!=display.planes)
  205.             {
  206.                 old_len  = c->num_planes * len;
  207.                 new_len = display.planes * len;
  208.             
  209.                 new_data = (short*)Mxalloc(new_len+10,MX_STRAM);
  210.                 
  211.                 for(l=0; l<(old_len+1)>>1; l++)
  212.                     new_data[l]=c->col_data[l];
  213.                 for(; l<(new_len+1)>>1; l++)
  214.                     new_data[l]=0;
  215.             
  216.                 c->sel_data=new_data;
  217.             }
  218.             
  219.             src.fd_w = icon->monoblk.ib_wicon;
  220.             src.fd_h = icon->monoblk.ib_hicon;
  221.             src.fd_wdwidth = icon->monoblk.ib_wicon>>4;
  222.             src.fd_stand = 1;
  223.             src.fd_r1 = src.fd_r2 = src.fd_r3 = 0;
  224.             src.fd_nplanes = display.planes;
  225.             dst = src;
  226.             dst.fd_stand = 0;
  227.             src.fd_addr = c->sel_data;
  228.             
  229.             vr_trnfm(V_handle,&src,&dst);
  230.         }
  231.     }
  232. }
  233.  
  234. /*
  235.     LoadResources : Load a GEM resource file
  236.     fname = name of file to load
  237.     Return = base pointer of resources or NULL on failure
  238. */
  239. void *LoadResources(char *fname,short designWidth,short designHeight)
  240. {
  241.     long err;
  242.     short handle;
  243.     RSHDR hdr;
  244.     OBJECT *obj,*tree;
  245.     TEDINFO *ti;
  246.     ICONBLK *ib;
  247.     CICONBLK *cib,**cibh;
  248.     BITBLK *bb;
  249.     CICON *cicn,*pcicn;
  250.     unsigned long osize,size,*index,*addr,*earray;
  251.     char *base,*ptext;
  252.     short i,j,type,numCibs=0,numRez,*pdata;
  253.  
  254.     resWidth = display.c_max_w;
  255.     resHeight = display.c_max_h;
  256.     
  257.     err = Fopen(fname,O_RDONLY);
  258.     if (err<0L)
  259.     {
  260.         DIAGS(("LoadResources():file not found\n"));
  261.         return NULL;
  262.     }
  263.     handle = (short)err;
  264.     Fread(handle,sizeof(RSHDR),&hdr);
  265.     size = (unsigned long)hdr.rsh_rssize;
  266.     osize = (size+1UL)&0xfffffffeUL;
  267.     if (hdr.rsh_vrsn&4)
  268.     { /* Extended format, get new 32-bit length */
  269.         Fseek(osize,handle,SEEK_SET);
  270.         Fread(handle,4L,&size);
  271.     }
  272.     Fseek(0L,handle,SEEK_SET);
  273.     base = (char*)Mxalloc(size+100,MX_STRAM);
  274.     if (!base)
  275.     {
  276.         Fclose(handle);
  277.         return NULL;
  278.     }
  279.     Fread(handle,size,base);
  280.     Fclose(handle);
  281.  
  282.     ti = (TEDINFO*)(base+(unsigned long)hdr.rsh_tedinfo);
  283.     for (i=0;i<hdr.rsh_nted;i++,ti++)
  284.     { /* Correct all tedinfo field pointers */
  285.         ti->te_ptext = (char*)((unsigned long)ti->te_ptext+base);
  286.         ti->te_ptmplt = (char*)((unsigned long)ti->te_ptmplt+base);
  287.         ti->te_pvalid = (char*)((unsigned long)ti->te_pvalid+base);
  288.     }
  289.  
  290.     ib = (ICONBLK*)(base+(unsigned long)hdr.rsh_iconblk);
  291.     for (i=0;i<hdr.rsh_nib;i++,ib++)
  292.     { /* Correct all iconblk field pointers */
  293.         ib->ib_pmask = (short*)((unsigned long)ib->ib_pmask+base);
  294.         ib->ib_pdata = (short*)((unsigned long)ib->ib_pdata+base);
  295.         ib->ib_ptext = (char*)((unsigned long)ib->ib_ptext+base);
  296.     }
  297.  
  298.     bb = (BITBLK*)(base+(unsigned long)hdr.rsh_bitblk);
  299.     for (i=0;i<hdr.rsh_nbb;i++,bb++) /* Correct all bitblk data pointers */
  300.         bb->bi_pdata = (short*)((unsigned long)bb->bi_pdata+base);
  301.     if (hdr.rsh_vrsn&4)
  302.     { /* It's an enhanced RSC file */
  303.  
  304.         earray = (unsigned long*)(osize+(long)base);
  305.  
  306.         cibh = (CICONBLK**)(earray[1]+(long)base);
  307.         if ((long)cibh>0L)
  308.         { /* Get colour icons */
  309.             while (*cibh++!=(CICONBLK*)-1L)
  310.                 numCibs++;
  311.  
  312.             cib = (CICONBLK*)cibh;
  313.             cibh = (CICONBLK**)(earray[1]+(long)base);
  314.             for (i=0;i<numCibs;i++)
  315.             { /* Fix up all the CICONBLK's */
  316.                 cibh[i] = cib;
  317.                 ib = &cib->monoblk;
  318.                 size = 2UL*(unsigned long)(ib->ib_wicon/16)*(unsigned long)ib->ib_hicon;
  319.                 addr = (unsigned long*)((long)cib+sizeof(ICONBLK));
  320.                 numRez = (short)*addr;
  321.                 pdata = (short*)&addr[1];
  322.                 ib->ib_pdata = pdata;
  323.                 ib->ib_pmask = (short*)((long)pdata+size);
  324.                 ptext = (char*)((long)pdata+size*2L);
  325.                 ib->ib_ptext = ptext;
  326.                 ptext[11] = 0;
  327.                 cicn = (CICON*)((long)ptext+12L);
  328.                 cib->mainlist = cicn;
  329.                 for (j=0;j<numRez;j++)
  330.                 { /* Get CICON's at different rez's */
  331.                     pcicn = cicn;
  332.                     pdata = (short*)((long)cicn+sizeof(CICON));
  333.                     cicn->col_data = pdata;
  334.                     cicn->col_mask = (short*)((long)pdata+size*(unsigned long)cicn->num_planes);
  335.                     if (cicn->sel_data!=NULL)
  336.                     { /* It's got a selected form */
  337.                         cicn->sel_data = (short*)((long)cicn->col_mask+size);
  338.                         cicn->sel_mask = (short*)((long)cicn->sel_data+size*(unsigned long)cicn->num_planes);
  339.                         cicn = (CICON*)((long)pcicn+sizeof(CICON)+2L*size*((unsigned long)pcicn->num_planes+1L));
  340.                     }
  341.                     else
  342.                     { /* No selected version */
  343.                         cicn->sel_data = cicn->sel_mask = NULL;
  344.                         cicn = (CICON*)((long)pcicn+sizeof(CICON)+size*((unsigned long)pcicn->num_planes+1L));
  345.                     }
  346.                     if (pcicn->next_res==(CICON*)1L)
  347.                         pcicn->next_res = cicn;
  348.                     else
  349.                         pcicn->next_res = NULL;
  350.                 }
  351.                 cib = (CICONBLK*)cicn;
  352.             }
  353.         }
  354.     }
  355.  
  356.     obj = (OBJECT*)(base+(unsigned long)hdr.rsh_object);
  357.     for (i=0;i<hdr.rsh_nobs;i++,obj++)
  358.     { /* Correct all objects' ob_spec pointers */
  359.         type = obj->ob_type&255;
  360.         switch (type)
  361.         { /* What kind of object is it? */
  362.             case G_TEXT:
  363.             case G_BOXTEXT:
  364.             case G_IMAGE:
  365.             case G_BUTTON:
  366.             case G_STRING:
  367.             case G_FTEXT:
  368.             case G_FBOXTEXT:
  369.             case G_ICON:
  370.             case G_TITLE:
  371.                 obj->ob_spec = (void*)((unsigned long)obj->ob_spec+base);
  372.                 break;
  373.             case G_CICON:
  374.                 FixColourIconData(cibh[(long)obj->ob_spec]);
  375.                 obj->ob_spec = cibh[(long)obj->ob_spec];
  376.                 break;
  377.             case G_PROGDEF:
  378.                 obj->ob_spec = NULL;
  379.                 break;
  380.             case G_IBOX:
  381.             case G_BOX:
  382.             case G_BOXCHAR:
  383.                 break;
  384.             default:
  385.                 Cconws("Unknown object type\r\n");
  386.                 break;
  387.         }
  388.     }
  389.  
  390.     index = (unsigned long*)(base+(unsigned long)hdr.rsh_trindex);
  391.     for (i=0;i<hdr.rsh_ntree;i++,index++)
  392.     {
  393.         tree = obj = (OBJECT*)(*index+(unsigned long)base);
  394.         if ((obj[3].ob_type&255)!=G_TITLE) /* Not a menu tree */
  395.             do { /* Fix all object coordinates */
  396.                 obj->ob_x = (((obj->ob_x&255)*designWidth+(obj->ob_x>>8))*resWidth)/designWidth;
  397.                 obj->ob_y = (((obj->ob_y&255)*designHeight+(obj->ob_y>>8))*resHeight)/designHeight;
  398.                 obj->ob_width = (((obj->ob_width&255)*designWidth+(obj->ob_width>>8))*resWidth)/designWidth;
  399.                 obj->ob_height = (((obj->ob_height&255)*designHeight+(obj->ob_height>>8))*resHeight)/designHeight;
  400.             } while (!(obj++->ob_flags&LASTOB));
  401.         else
  402.         { /* Standard AES menu */
  403.             j = 0;
  404.             do { /* Use conventional AES routine */
  405. /*                rsrc_obfix(tree,j);        // GEMAES */
  406.             } while (!(tree[j++].ob_flags&LASTOB));
  407.         }
  408.     }
  409.     return (void*)base;
  410. }
  411.  
  412.  
  413. /*
  414.     FreeResources : Dispose of a set of loaded resources
  415.     base = pointer to base of resources
  416. */
  417. void FreeResources(void *base)
  418. {
  419.     Mfree(base);
  420. }
  421.  
  422.  
  423. /*
  424.     ResourceTree : Find the tree with a given index
  425.     base = pointer to base of resources
  426.     num = index number of tree
  427.     Return = pointer to tree or NULL on failure
  428. */
  429. OBJECT *ResourceTree(void *base,short num)
  430. {
  431.     unsigned long *index,offset;
  432.     RSHDR *hdr = (RSHDR*)base;
  433.  
  434.     if ((!hdr)||(num<0)||(num>=hdr->rsh_ntree))
  435.         return NULL;
  436.     index = (unsigned long*)((unsigned long)base+(unsigned long)hdr->rsh_trindex);
  437.     offset = index[num];
  438.     return (OBJECT*)((unsigned long)base+offset);
  439. }
  440.  
  441. /* ResourceString : Find the string with a given index
  442.     base = pointer to base of resources
  443.     num = index number of string
  444.     Return = pointer to string or NULL on failure
  445. */
  446. char *ResourceString(void *base,short num)
  447. {
  448.     unsigned long *index,offset;
  449.     RSHDR *hdr = (RSHDR*)base;
  450.  
  451.     if (!(hdr)||(num<0)||(num>=hdr->rsh_nstring))
  452.         return(NULL);
  453.     index = (unsigned long*)((unsigned long)base+(unsigned long)hdr->rsh_frstr);
  454.     offset = index[num];
  455.     return((char*)((unsigned long)base+offset));
  456. }
  457.  
  458. /*
  459.     ResourceImage : Find the image with a given index
  460.     base = pointer to base of resources
  461.     num = index number of image
  462.     Return = pointer to image or NULL on failure
  463. */
  464. void *ResourceImage(void *base,short num)
  465. {
  466.     unsigned long *index,offset;
  467.     RSHDR *hdr = (RSHDR*)base;
  468.  
  469.     if ((!hdr)||(num<0)||(num>=hdr->rsh_nimages))
  470.         return(NULL);
  471.     index = (unsigned long*)((unsigned long)base+(unsigned long)hdr->rsh_frimg);
  472.     offset = index[num];
  473.     return((void*)((unsigned long)base+offset));
  474. }
  475.